import { HealthCheckWithScreenTask, V3proLastErrCache } from "./health.check.class";
import { SendboxKey, V3proSubjectMessage } from "../model/v3pro.model";
import { V3proAlertMessageErrTypeEnum } from "../model/v3pro.enum";
import { V3proAllStatusLambda } from "../helper/v3pro.all.status.lambda.helper";
import { V3proScreenLayoutHelper } from "../helper/v3pro.screen.layout.helper";
import { V3proScreen, V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { V3proCheckSendboxNetworkLinkTask } from "./v3pro.check.sendbox.network.link.task";
import { appConfig } from "../../../configs/env.config.load";
import { complement } from "ramda";

/**
 * 发送盒通信故障
 *
 * 是否支持阈值设置： N
 *
 * 查询周期:
 * 定时查询
 * 间隔1秒
 * 持续30秒
 * 连续5次失败
 *
 * 告警产生的原因:
 * 所有UDP包通信无响应
 * 发送盒供电异常或者发送盒网口与网线未连接
 *
 * 异常现象:
 * 发送盒带载区域显示花屏
 * 发送盒带载区域显示亮度色彩无法控制
 *
 * 维修建议:
 * 依次排查发送盒控制网线、发送盒电源等
 *
 * 告警级别: 次要
 */
const errCache = new V3proLastErrCache<SendboxKey>();
export class V3proCheckSendboxCommTask extends HealthCheckWithScreenTask<SendboxKey[]> {
  checkResult = this.__checkSendBoxResult.bind(this);

  async genKeyParams(
    allStatus: V3proScreenAllStatus,
    screen: V3proScreen,
    v3proScreenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<SendboxKey[]> {
    const [errs, normals] = V3proAllStatusLambda.filterSendBoxesWithRemove(
      allStatus,
      complement(V3proAllStatusLambda.communicationOK),
    );
    this.__startChildTask(new V3proCheckSendboxNetworkLinkTask(allStatus, v3proScreenLayoutHelper));
    return errCache.mergeCache(
      errs.map(SendboxKey.build),
      normals.map(SendboxKey.build),
      screen.id,
      (s) => s.id,
    );
  }

  genSRMqttTopic(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): string {
    return `notify/alert/ssads/device-led-disp:controller-internal-err/${screenId}`;
    // return "发送盒通讯故障";
  }

  getV3proAlertMessage(): V3proAlertMessageErrTypeEnum {
    return V3proAlertMessageErrTypeEnum.SEND_BOX_COMM;
  }

  isNeedUploadFile(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): boolean {
    return message.isErrStatus();
  }

  isReportEnabled(keyParams: any, screenId: string): boolean {
    return appConfig.v3pro.errEnable.sendBoxComm;
  }
}
